﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class StateMachineEventELBDao : DaoService<TEvent>
    {
        public override string TableName => "FAS_StateMachineEvent";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "StateMachineID";
        public string MetaDataForeignColumn => "StateMachineID";
        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<TEvent>();

        public StateMachineEventELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="smId"></param>
        /// <returns></returns>
        public List<TEvent> GetTEventsByForeignKey(string smId)
        {
            var result = new List<TEvent>();
            if (string.IsNullOrEmpty(smId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{smId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as TEvent));
            }
            return result.OrderBy(o => o.Sort).ToList();
        }

        /// <summary>
        /// 添加TEvent
        /// </summary>
        /// <param name="events"></param>
        public void CreateTEvent(List<TEvent> events)
        {
            try
            {
                DaoUtil.ExecuteInsertSQL(events, this);    //创建事件
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.CreateEventError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除TEvent
        /// </summary>
        /// <param name="smId"></param>
        public void DeleteTEventByForeignKey(string smId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, smId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteEventError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除TAction
        /// </summary>
        /// <param name="smId"></param>
        public void DeleteByMetaDataForeignKey(string smId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, MetaDataForeignColumn, smId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteActionError, e.Message, e);
            }
        }
    }
}
